import { httpService } from './http.service';
import type { ApiResponse, PaginatedResponse } from '@/types/common/api';

/**
 * 基础API服务类
 * 提供通用的API调用方法
 */
export abstract class BaseApiService {
  protected baseUrl: string;

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  protected async get<T>(endpoint: string = ''): Promise<T> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.get<T>(url);
    return response.data || (response as T);
  }

  protected async post<T>(endpoint: string = '', data?: any): Promise<T> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.post<T>(url, data);
    return response.data || (response as T);
  }

  protected async put<T>(endpoint: string = '', data?: any): Promise<T> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.put<T>(url, data);
    return response.data || (response as T);
  }

  protected async delete<T>(endpoint: string = ''): Promise<T> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.delete<T>(url);
    return response.data || (response as T);
  }

  protected async query<T>(endpoint: string = '', params?: any): Promise<T> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.query<T>(url, params);
    return response.data || (response as T);
  }

  protected async getPaginated<T>(
    endpoint: string = '',
    params?: any,
  ): Promise<PaginatedResponse<T>> {
    const url = endpoint ? `${this.baseUrl}/${endpoint}` : this.baseUrl;
    const response = await httpService.query<PaginatedResponse<T>>(url, params);
    return response.data || (response as unknown as PaginatedResponse<T>);
  }
}
